package com.service.socket.process;
/****
 * 对subSpeedTimeMap缓存重新刷新赋值
 */
import org.jboss.netty.channel.Channel;

import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.socket.protocol.CmdWord;
import com.service.socket.protocol.Conversion;
import com.service.socket.protocol.Packet;

public class R_Class_FB extends R_Class_Abstract {
	public R_Class_FB(Channel channel, Packet packet) {
		super(channel, packet);
	}

	@Override
	public void execute() {
		try {
			refreshSubSpeedTimeMap();
		} catch (Exception e) {
			Logger.err(e);
		}		
	}

	private void refreshSubSpeedTimeMap() {
		if (!packet.isCrcValid()) {
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device [" + packet.getDeviceNo()+ "] regist fail on [isCrcValid]:" + packet.isCrcValid());
			return;
		}
		getLastSignalsByJson(packet.dataArea);
		// 数据接收成功
		Packet bean = (Packet) packet.clone();
		bean.status = CmdWord.STATUS_SUCCESS;
		bean.dataArea = null;
		SendCommand.replyDtu_E0_NOSAVE(channel,bean);
	}
	//数据区的gpsno,包含多辆车
	public void getLastSignalsByJson(String dataArea){
		String str = new String(Conversion.hexStringToBytes(dataArea));
		String[] devices = str.split(",");
		for(String deviceNo:devices){
			vehicle = CacheManager.instance().getVehicle(deviceNo);
			//刷新数据库
			if(vehicle==null||vehicle.device==null||vehicle.device.deviceNo==null){
				Logger.err(deviceNo+"查无此车,无法解除报警");
				continue ;
			}
			CacheManager.instance().refreshSubSpeedTimeMap(vehicle);
			CacheManager.instance().subSpeedMap.put(deviceNo, 0L);
		}
	}
}
